﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.IO;

namespace h2100
{
    public class hActionManager:hEntityManager
    {
        List<hActionButton> _lstCurrentActionButtons;
        hText _console;

        private hVisibleGameEntity _actionCaller;
        public hVisibleGameEntity ActionCaller
        {
            get { return _actionCaller; }
            set { _actionCaller = value; }
        }

        public void setCurrentActionButtons(List<hActionButton> lst)
        {
            _lstCurrentActionButtons.Clear();
            if (lst != null)
            {
                
                _lstCurrentActionButtons.AddRange(lst);
            }
        }

        /// <summary>
        /// Đọc tập tin cấu hình cho các action button
        /// </summary>
        /// <param name="content"></param>
        /// <param name="_args"></param>
        /// <param name="fileName">Tên file cấu hình các action ("Action.hcf")</param>
        public hActionManager(ContentManager content, hParams _args, String fileName)        
        {
            _console = new hText(content);
            _lstCurrentActionButtons = new List<hActionButton>();
            StreamReader reader = new StreamReader(fileName);

            _nprototype = int.Parse(reader.ReadLine())+1;
            _prototype = new hActionButton[_nprototype];   //Bỏ vị trí 0

            for (int i = 1; i < _nprototype; i++)
            {
                string name = reader.ReadLine().Split('#')[1];
                string texture = reader.ReadLine().Split('=')[1];
                int cost = int.Parse(reader.ReadLine().Split('=')[1]);
                string action = reader.ReadLine().Split('=')[1];
                int[] subactions = actionParse(reader.ReadLine().Split('=')[1].Split(' '));
                int maxlevel = int.Parse(reader.ReadLine().Split('=')[1]);
                string tmp = reader.ReadLine();
                int posX = int.Parse(tmp.Split('=')[1].Split(' ')[0]);
                int posY = int.Parse(tmp.Split('=')[1].Split(' ')[1]);
                hActionButton temp = new hActionButton(content, texture, posX, posY,_console);
                temp.TooltipText = name;
                temp.Cost = cost;
                temp.ActionString = action;
                temp.SubActionsIndexes = subactions;
                temp.MaxLevel = maxlevel;   
             
                _prototype[i]=temp;
            }
            
            reader.Close();
        }

        public new hActionButton createObject(int idx)
        {
            if (idx < 0 || idx >= _nprototype)
                return null;
            hActionButton  s = (hActionButton)_prototype[idx].clone();
            // Gắn các subaction vào, bỏ qua các subaction có subaction
            if (s.SubActionsIndexes != null)
            {
                for (int i = 0; i < s.SubActionsIndexes.Count(); i++)
                {
                    hActionButton temp = (hActionButton)_prototype[s.SubActionsIndexes[i]].clone();
                    if (temp.SubActionsIndexes == null)
                    {
                        s.SubActionsList.Add(temp);
                    }
                }
            }
            return s;
        }

        public void update(hParams args,GameTime gameTime)
        {
            for (int i = 0; i < _lstCurrentActionButtons.Count; i++)
            {
                _lstCurrentActionButtons[i].update(args, gameTime);
            }
        }

        public void draw(hParams args,GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _lstCurrentActionButtons.Count; i++)
            {
                _lstCurrentActionButtons[i].draw(gameTime, spriteBatch);
            }

            args.Skill.draw(gameTime, spriteBatch);
        }
    }
}
